<?php
Yii::import('zii.widgets.grid.CGridView');
Yii::import('application.components.widgets.CGridView.*');
class CGridViewPlus extends CGridView{
	public $toolsCssClass='';
	public $template="{searchForm}\n{headerTool}\n{items}\n{pager}";
	
	public $headerTool = array();
	public $searchForm = array();
	public $cacheId;
	public function init(){
		$this->cssFile = Yii::app()->resource->path('css')."/widget/widget-grid.css";		
		$this->itemsCssClass='table grid-table';
		$this->pager=array(
			'prevPageLabel'=>"<",
			'nextPageLabel'=>'>',
			'maxButtonCount'=>10,
			'previousPageCssClass'=>'prev',
			'nextPageCssClass'=>'next',
		);
		$this->htmlOptions['class'] = 'widget-grid';
		$this->htmlOptions['id'] = $this->id;
		$this->searchForm['widget'] = 'application.components.widgets.CGridView.GridSearchForm';
		$this->setCache();
		parent::init();
	}
	
	public function setCache(){
		$now_valid=md5(json_encode($this->columns));
		$old_valid=Yii::app()->cache->get($this->id.'-validate');
		if(!Yii::app()->cache->get($this->id.'-config')){
			Yii::app()->cache->set($this->id.'-config',$this->columns);
		}
		if($now_valid!==$old_valid){
			Yii::app()->cache->set($this->id.'-validate',md5(json_encode($this->columns)));
			Yii::app()->cache->set($this->id.'-config',$this->columns);
		}
	}
	
	public function renderSearchForm(){
		if (!empty($this->searchForm)) {
			$element = array();
			if (array_key_exists('element', $this->searchForm)) {
				foreach ($this->searchForm['element'] as $elementKey => $elementConfig){
					$element[$elementKey] = $elementConfig;
				}
			}
						
			
			$form=$this->beginWidget($this->searchForm['widget'],
				array(
					'action'		=>	Yii::app()->baseUrl.'/dashboard/admin',
			 		'htmlOptions'	=>	array('class'=>'grid-form form-inline'),
					'method'		=>	'get',
					'ajaxSubmit'	=>	array('qerqe'),
				)
			);
			$this->render('application.components.widgets.CGridView._searchForm',array(
				'form' 		=> $form,
				'element'	=> $element,	
				'model'		=> $this->dataProvider->model
			));
			
			$this->endWidget();
			
			if ($form->ajaxSubmit !== null) {
				Yii::app()->clientScript->registerScript('search', "
					$(document).on('click','.grid-form .grid-form-submit',function(){
						$(this).parents('.grid-form').submit();
					});
					
					$(document).on('submit','.grid-form',function(){
						$.fn.yiiGridView.update('{$this->getId()}', {
							data : $(this).serialize(),
							type : 'get',
						});
						return false;
					});
				");
			}
			
		}
	}
	
	public function renderHeaderTool(){
		if (!empty($this->headerTool)){
			$headerToolHtml = null;
			$buttonHtmlOptions= array();
			$htmlOptions = array(
				'class' => 'grid-header-tool',
			);
			if (array_key_exists('class', $this->headerTool)) {
				$htmlOptions['class'] = $this->headerTool['class'];
			}
			
			if  (array_key_exists('buttonClass', $this->headerTool)) {
				$buttonHtmlOptions['class'] = $this->headerTool['buttonClass'];
			}
			
			if (array_key_exists('buttons', $this->headerTool)) {
				$buttonHtmls = array();
				foreach ($this->headerTool['buttons'] as $toolButton){
					$button = new GridToolButton();
					$button->setPrototype($this);
					$button->label	= $toolButton['label'];
					$button->type	= !empty($toolButton['type'])?$toolButton['type']:'link';
					$button->url	= (!empty($toolButton['url']))?$toolButton['url']:'';
					$button->htmlOptions = $buttonHtmlOptions;
					$buttonHtml = $button->button();
					array_push($buttonHtmls,$buttonHtml);
				}
				$headerToolHtml .= implode(' ',$buttonHtmls);
			}
						
			echo CHtml::tag('div',$htmlOptions,$headerToolHtml);
			
		}
		
	}
		
	
	
}